package com.fuyun.rpc.common.serializer;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author: zrq1351
 * @date: 2025/2/15
 */
public class HessianSerializer implements Serializer {
	@Override
	public <T> byte[] serialize(T object) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		HessianOutput ho = new HessianOutput(bos);
		try {
			ho.writeObject(object);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return bos.toByteArray();
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T deserialize(byte[] bytes, Class<T> tClass) {
		ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
		HessianInput hi = new HessianInput(bis);
		try {
			return (T)hi.readObject(tClass);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}